package com.atguigu.gulimall.order.controller;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @program: gulimall
 * @description:
 * @author: 江天赐
 * @create: 2022-08-16 10:06
 */
@Slf4j
@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMq")
    public void sendMessage() {
        //String exchange, 交换机
        // String routingKey, 路由键
        // Object message, 消息,可以是一个任意类型对象
        // MessagePostProcessor messagePostProcessor,
        // @Nullable CorrelationData correlationData
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                String msg = "Hello World!";
                OrderReturnReasonEntity orderReturnReasonEntity = new OrderReturnReasonEntity();
                orderReturnReasonEntity.setCreateTime(new Date());
                orderReturnReasonEntity.setId(1L);
                orderReturnReasonEntity.setName("haha-" + i);
                rabbitTemplate.convertAndSend("java.exchange", "hello.java", orderReturnReasonEntity, new CorrelationData(UUID.randomUUID().toString()));
//                log.info("消息发送完成{}", orderReturnReasonEntity);
            } else {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setOrderSn(UUID.randomUUID().toString());
                rabbitTemplate.convertAndSend("java.exchange", ".java", orderEntity, new CorrelationData(UUID.randomUUID().toString()));
//                log.info("消息发送完成{}", orderEntity);
            }
        }
    }
}